-----------------------------------------------------------------------
-- File:        Console_Controller.adb
-- Description: User interface to the train control system,
--              key presses are translated into train control commands
--
-- Requires:    Io_Ports,Unsigned_Types,Raildefs,Dio192defs, Dda06defs,
--              Int32defs,Dac_Controller,Blocks,Turnout_Controller,Simrail2
--              Swindows,Env_Monitor,Topolog2,Controllers,Hand_Controller
--              ProjDefs
--
-- Author:     Maria Mitrevska, Ha Le
-----------------------------------------------------------------------
with Dac_Controller;
with Blocks;
with Turnout_Controller;
with Simrail2; --*
with Io_Ports;
with Swindows;
use Swindows;
with Unsigned_Types;
use Unsigned_Types;  -- 1.8
with Raildefs,
   Dio192defs, Dda06defs, Int32defs, Halls2;
use Raildefs;
with Interrupt_Hdlr;
with Env_Monitor;
with Topolog2;
use Topolog2;
with Controllers;
with ProjDefs;
use ProjDefs;
with Heart_Beat;
use Heart_Beat;
with Hand_Controller;
--with handheld_controller;
--package body Console_Controller is

procedure Console_Controller is--ACTIVE

   -- package Iio renames Ada.Integer_Text_Io;
   W_In,
   W_Info,
   W_Interrupts : Swindows.Window;

   Trains_Data          : Simrail2.Init_Array;          --*
   Trains_Init_Position : ProjDefs.Train_Init_Position;
   Debug_Wins           : Debug_Array;

   --Init registers, turnouts and blocks
   procedure Init_Dio is
      use Dio192defs;
   begin
      Turnout_Controller.Init_Turnouts;
      Blocks.Init_Block;
   end Init_Dio ;

   --Reset position of trains
   procedure Reset_Trains is
   begin
      --Simrail2.Reset(N_Trains            => 3,N_Carriages_Train_1 => 1);
      Trains_Data := ((1,0,43,50.0,"Percy     "), --*
         (2,0,51,50.0,"Bill      "),--*
         (3,0,25,100.0,"Toby      "),--*
         (4,0,0,20.0, "Diesel    ")) ;--*
      Simrail2.Reset(3,  --*
         Trains_Data);  -- north siding--*
      Swindows.Open(W_In,0,0, 7,75, "Input");
      Swindows.Open(Debug_Wins(1),8,0, 23,19, "Interrupts 1"); -- 24 lines
      Swindows.Open(Debug_Wins(2),8,20, 23,39, "Interrupts 2");
      -- 24 lines
      Swindows.Open(Debug_Wins(3),8,40, 23,59, "Interrupts 3");
      -- 24 lines
      Swindows.Open(Debug_Wins(4),8,60, 23,79, "Interrupts 4");
      -- 24 lines

      --Put_Line(W_Interrupts, "Vevery begining");
      -- This is when real trains are used
      Trains_Init_Position := (
         43,
         51,
         25,
         0); -- Sensors right before trains


      Controllers.Init_W_Int(Debug_Wins);
      --Interrupt_Hdlr.Init(W_Interrupts);

      Controllers.Init_Trains(Trains_Init_Position);
      Hand_Controller.Init_W_Int(Debug_Wins(4));

      Turnout_Controller.Init_Turnouts;

   end Reset_Trains;


   --Dialog loop
   procedure Dialog_Loop is
      C,
      T : Character;
   begin
      loop
          Put_Line(W_In, "Command: Forward(q/t/i)   Backward(z/b/,)   Faster(w/y/o)   Slower(x/n/.)");
          Put_Line(W_In, "Left(a/g/k)      Right(d/j/;)    Centre(s/h/l)");
          Put_Line(W_In, "T1 Mode(1u/1m)   T2 Mode(2u/2m)  T3 Mode(3u/3m)");


         Get_Char(W_In, C);
         case C is

            --forward
            when 'q' =>
               Controllers.Start_Train_Forward (1);
            when 't' =>
               Controllers.Start_Train_Forward (2);
            when 'i' =>
               Controllers.Start_Train_Forward (3);

            --backward
            when 'z' =>
               Controllers.Start_Train_Backward (1);
            when 'b' =>
               Controllers.Start_Train_Backward (2);
            when ',' =>
               Controllers.Start_Train_Backward (3);

            --faster
            when 'w' =>
               Controllers.Start_Fast(1);
            when 'y' =>
               Controllers.Start_Fast(2);
            when 'o' =>
               Controllers.Start_Fast(3);
            when '=' =>
               Controllers.Start_Fast(4);
            --Ada.Text_IO.Put_Line("TRAM HERE");
            --passed
            --slower
            when 'x' =>
               Controllers.Start_Slow(1);
            when 'n' =>
               Controllers.Start_Slow(2);
            when '.' =>
               Controllers.Start_Slow(3);
            when '-' =>
               Controllers.Start_Slow(4);

            --turn left
            when 'a' =>
               Controllers.Start_Train_Direction_Left (1);
            when 'g' =>
               Controllers.Start_Train_Direction_Left (2);
            when 'k' =>
               Controllers.Start_Train_Direction_Left (3);


            --turn right
            when 'd' =>
               Controllers.Start_Train_Direction_Right (1);
            when 'j' =>
               Controllers.Start_Train_Direction_Right (2);
            when ';' =>
               Controllers.Start_Train_Direction_Right (3);


            --centre
            when 's' =>
               Controllers.Start_Train_Direction_Centre (1);
            when 'h' =>
               Controllers.Start_Train_Direction_Centre (2);
            when 'l' =>
               Controllers.Start_Train_Direction_Centre (3);

            --halt
            when 'c' =>
               Controllers.Start_Halt(1);
            when 'm' =>
               Controllers.Start_Halt(2);
            when '/' =>
               Controllers.Start_Halt(3);


            --horn
            when 'e' =>
               null;

            when 'u' =>
               null;

            when 'p' =>
               null;

            --train1
            when '1' =>
               Put_Line(W_In, "Option");
               Get_Char(W_In, T);
               case T is
                  when 'u' =>
                     Controllers.Start_Unmanned(1);

                  when 'm' =>
                     Controllers.Start_Manned(1);

                  when 'd' =>
                     null;

                  when others =>
                     null;

               end case;

            --train2

            when '2' =>
               Put_Line(W_In, "Option");
               Get_Char(W_In, T);
               case T is
                  when 'u' =>
                     Controllers.Start_Unmanned(2);

                  when 'm' =>
                     Controllers.Start_Manned(2);

                  when 'd' =>
                     null;

                  when others =>
                     null;

               end case;


            --train3
            when '3' =>
               Put_Line(W_In, "Option");
               Get_Char(W_In, T);
               case T is
                  when 'u' =>
                     Controllers.Start_Unmanned(3);

                  when 'm' =>
                     Controllers.Start_Manned(3);

                  when 'd' =>
                     null;

                  when others =>
                     null;

               end case;

            --train4
            when '4' =>
               Put_Line(W_In, "Option");
               Get_Char(W_In, T);
               case T is
                  when 'd' =>
                     null;

                  when others =>
                     null;

               end case;

            --dump
            when 'D' =>
               -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                Simrail2.Dump; --*
               null;
            --reset
            when 'r' =>
               Reset_Trains;

            when others =>
               null;
         end case;
         Clear(W_In);
      end loop;
   end Dialog_Loop;

begin
   --Init IO Blocks Gone here
   Init_Dio;

   Reset_Trains;

   Halls2.Initialize;
   Env_Monitor.Install; -- calls Halls2
   Dialog_Loop;

end Console_Controller;

--end Console_Controller;

